package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 * @author Fengkai
 * @creat 2023/4/12
 * 输入描述：
 * 1、谜面单词列表，以","分隔
 * <p>
 * 2、谜底库单词列表，以","分隔
 * <p>
 * 输出描述：
 * 匹配到的正确单词列表，以“,”分隔 如果找不到，返回"not found"
 * 补充说明：
 * 1、单词的数量 N 的范围：0 < N < 1000
 * 2、词汇表的数量 M 的范围： 0 < M < 1000
 * 3、单词的长度 P 的范围：0 < P < 20
 * 4、输入的字符只有小写英文字母，没有其它字符
 */
public class GuessStringGame {

    public static void main (String[] args) {
        System.out.println(guessString("bdni,wooood", "bind,wrong,wood"));

    }

    public static String guessString (String miMian, String miDi) {
        String[] miMainArray = miMian.split(",");
        String[] miDiArray = miDi.split(",");
        ArrayList<String> ans = new ArrayList<String>();
        for (int i = 0; i < miMainArray.length; i++) {
            String mi = sortAndRemove(miMainArray[i]);
            for (int j = 0; j < miDiArray.length; j++) {
                String anstring = sortAndRemove(miDiArray[j]);
                if (anstring.equals(mi)) {
                    ans.add(miDiArray[j]);
                }
            }
        }
        if (ans.size() == 0) {
            return "not found";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ans.size(); i++) {
            sb.append(ans.get(i));
            if (i != ans.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    private static String sortAndRemove (String str) {
        TreeSet<Character> characters = new TreeSet<>();
        for (int i = 0; i < str.length(); i++) {
            characters.add(str.charAt(i));
        }
        StringBuilder sb = new StringBuilder();
        for (Character character : characters) {
            sb.append(character);
        }
        return sb.toString();
    }
}
